/**
 * 
 */
package com.nari.interfaces.action;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.interfaces.service.IPdaUploadService;
import com.nari.platform.date.CalendarConstants;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.ResponseMsg;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-11-13
 */
public class PdaUploadAction extends JSONActionSupport {
	private static final long serialVersionUID = 4406273564277330940L;
	private IPdaUploadService iPdaUploadService;
	//数据文件附件上传
	private File attachFile;
	private String attachFileFileName;
	
	/**
	 * 基于附件上传的掌机文件上传
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-14
	 */
	public String uploadPdaData() throws Exception {
		this.rmsg = iPdaUploadService.saveDataFileUpload(attachFile, attachFileFileName);
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 基于流文件的掌机文件上传
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-17
	 */
	public String uploadPdaStreamData() throws Exception {
	  	//附件的上传路径
	  	File uploadFolder = new File(this.getTempFileFolder());
	  	if(!uploadFolder.exists()) {
	  		uploadFolder.mkdirs();
	  	}
	  	//定义XML文件名
	  	String ranFileName = CalendarUtils.parseDateToString(new Date(), CalendarConstants.dateTimeMiliPreciseFormat_stamp);
	  	String absolutePath = this.getTempFileFolder() + "/" + ranFileName + ".xml";
	  	//缓存掌机数据
	  	StringBuffer xmlString = new StringBuffer("");
	  	//定义文件输入输出流
	  	BufferedReader br = null;
	  	BufferedWriter bw = null;
	    try {
	    	//获取底层Request
		  	HttpServletRequest request = SysEnvironmentMgr.getRequestInstance();
			br = new BufferedReader(new InputStreamReader(request.getInputStream()));
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(absolutePath)));
			String readLine = "";
			while ((readLine = br.readLine()) != null) {
				bw.append(readLine);
				xmlString.append(readLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(br != null) br.close();
			if(bw != null) bw.close();
		}
	    if("".equals(xmlString.toString().trim()) 
	    		|| xmlString.toString().startsWith("<project>") 
	    		|| !xmlString.toString().endsWith("</project>")) {
	    	this.rmsg = ResponseMsg.getInstance(false, "不完整的数据文件，请重新上传！");
	    } else {
	    	//XML文件解析
		    this.attachFile = new File(absolutePath);
		    this.rmsg = iPdaUploadService.saveDataFileUpload(attachFile, null); 
	    }
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iPdaUploadService
	 */
	@JSON(serialize=false)
	public IPdaUploadService getiPdaUploadService() {
		return iPdaUploadService;
	}

	/**
	 * @param iPdaUploadService the iPdaUploadService to set
	 */
	public void setiPdaUploadService(IPdaUploadService iPdaUploadService) {
		this.iPdaUploadService = iPdaUploadService;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

}
